#include<bits/stdc++.h>
using namespace std;

bool lower(string a,string b){
    if(a.length()<b.length()) return true;
    if(a.length()>b.length()) return false;
    return a.compare(b)<0;
}

struct Node{
    string state;
    string op;
    Node(){}
    Node(string state,string op){
        this->state=state;
        this->op=op;
    }
    bool operator<(const Node& node)const{
        return !lower(op,node.op);
    }
};

unordered_map<string,string> ump;

int main(){
    string statee;
    for(int i=1;i<=8;i++){
        int num;
        cin>>num;
        statee+=to_string(num);
    }
    //cout<<"statee"<<statee<<endl;
    priority_queue<Node> que;
    que.push(Node("12345678",""));
    ump["12345678"]="";
    while(!que.empty()){
        Node node=que.top();
        que.pop();
        string sta=node.state;
        string opc=node.op;
        if(lower(ump[sta],opc)) continue;
        //cout<<"que"<<sta<<" "<<opc<<endl;
        //A B C
        for(int i=0;i<3;i++){
            vector<int> arr[3]={
                {7,6,5,4,3,2,1,0},
                {3,0,1,2,5,6,7,4},
                {0,6,1,3,4,2,5,7}
            };
            string stan="";
            for(int j=0;j<8;j++){
                stan+=sta[arr[i][j]];
            }
            string opn=opc+(char)('A'+i);
            if(!ump.count(stan)||lower(opn,ump[stan])){
                que.push(Node(stan,opn));
                ump[stan]=opn;
            }
        }
    }
    cout<<ump[statee].length()<<endl;
    string str=ump[statee];
    for(int i=0;str[i]!='\0';i++){
        cout<<str[i];
        if((i+1)%60==0) cout<<endl;
    }
    return 0;
}